package com.example.eddie.test.MyAdapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.eddie.test.PoJo.News;
import com.example.eddie.test.R;

import java.util.List;

/**
 * Created by eddie on 28/08/2017.
 */

public class NewsAdapter extends ArrayAdapter {

    private Context context;
    private int resource;
    private List<News> list;

    public NewsAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<News> objects) {
        super(context, resource, objects);

        this.context = context;
        this.resource = resource;
        this.list = objects;
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {

        if (view == null){
            view = LayoutInflater.from(context).inflate(resource,parent,false);
        }

        News news = list.get(position);

        LayoutHandler handler = new LayoutHandler(view);

        handler.name.setText(news.getName());
        handler.time.setText("["+news.getDate());

        Glide.with(context)
                .load("http://59.79.112.9/Images/home/index_news_dot.gif")
                .into(handler.show);

        return view;
    }

    class LayoutHandler{
        TextView name;
        ImageView show;
        TextView time;

        public LayoutHandler(View view){
            name = view.findViewById(R.id.name);
            show = view.findViewById(R.id.head_image);
            time = view.findViewById(R.id.time);
        }
    }
}
